﻿#include"game.h"

void menu()
{
	printf("**************************\n");
	printf("*-------三子棋游戏-------*\n");
	printf("**************************\n");
	printf("********1.开始游戏********\n");
	printf("********0.退出游戏********\n");
	printf("**************************\n");
}

void game()
{
	char arr[ROW][COL];
	char ret = 0;
	InitBoard(arr, ROW, COL);//初始化棋盘
	while (1)
	{
		system("cls");
		PrintBoard(arr, ROW, COL);//打印棋盘
		YouPlay(arr, ROW, COL);
		ComputerPlay(arr, ROW, COL);
		ret = WinPlay(arr, ROW, COL);
		if (ret != '@')
		{
			break;
		}
	}
	system("cls");
	if (ret == '*')
	{
		printf("玩家赢！\n");
	}
	else if(ret == '#')
	{
		printf("电脑赢！\n");
	}
	else if(ret == '$')
	{
		printf("平局！\n");
	}
	PrintBoard(arr, ROW, COL);//打印棋盘
	system("pause");
}



void InitBoard(char arr[ROW][COL], int row, int col)//初始化棋盘
{
	int i = 0;
	int k = 0;
	for (i = 0; i < ROW; i++)
	{
		for (k = 0; k < COL; k++)
		{
			arr[i][k] = ' ';//赋值空格
		}
	}
}


void PrintBoard(char arr[ROW][COL], int row, int col)//打印棋盘
{
	int i = 0;
	int k = 0;
	for (i=0; i < ROW; i++)
	{
		for (k = 0; k < COL; k++)
		{
			printf(" %c ", arr[i][k]);
			if (k < col - 1)
			{
				printf("|");
			}
		}
		printf("\n");
		if (i < row - 1)
		{
			for (int j = 0; j < COL; j++)
			{
				printf("---");
				if (j < col - 1)
				{
					printf("|");
				}
			}
		}
		printf("\n");
	}
}


void YouPlay(char arr[ROW][COL], int row, int col)//玩家下棋
{
	int x = 0, y = 0;
	while (1)
	{
		printf("玩家阶段(棋坐标)>");
		scanf("%d %d", &x, &y);
		if (x > 0 && x <= row && y >= 0 && y <= col)
		{
			if (arr[x-1][y-1] == ' ')
			{
				arr[x-1][y-1] = '*';
				break;
			}
			else
			{
				printf("棋位已有棋！请重新输入！\n");
			}
		}
	}
}


void ComputerPlay(char arr[ROW][COL], int row, int col)//电脑下棋
{
	int x = 0, y = 0;
	while (1)
	{
		srand((unsigned int)(time(NULL)));
		x = rand() % row;
		y = rand() % col;
		if (x > 0 && x <= row && y >= 0 && y <= col)
		{
			if (arr[x - 1][y - 1] == ' ')
			{
				arr[x - 1][y - 1] = '#';
				break;
			}
		}
	}
}

char WinPlay(char arr[ROW][COL], int row, int col)//判断输赢--三子棋算法
{
	//判断特殊情况
	//行
	for (int i = 1; i < ROW-1; i++)
	{
		//第一行
		if (arr[0][i - 1] == arr[0][i] && arr[0][i] == arr[0][i + 1]&&arr[0][i] != ' ')
		{
			return arr[0][i];
		}
		//最后一行
		if (arr[row-1][i - 1] == arr[row - 1][i] && arr[row - 1][i] == arr[row - 1][i + 1]&& arr[row - 1][i] != ' ')
		{
			return arr[row - 1][i];
		}
	}
	//列
	for (int k = 1; k < COL - 1; k++)
	{
		//第一列
		if (arr[k - 1][0] == arr[k][0] && arr[k][0] == arr[k + 1][0]&&arr[k][0] != ' ')
		{
			return arr[k][0];
		}
		//最后一列
		if (arr[k - 1][col-1] == arr[k][col - 1] && arr[k][col - 1] == arr[k + 1][col - 1]&& arr[k][col - 1] != ' ')
		{
			return arr[k][col - 1];
		}
	}

	for (int i = 1; i < ROW - 1; i++)
	{
		for (int k = 1; k < COL - 1; k++)
		{
			//判断行
			if (arr[i][k - 1] == arr[i][k] && arr[i][k] == arr[i][k + 1] && arr[i][k] != ' ')
			{
				return arr[i][k];
			}
			//判断列
			if (arr[i - 1][k] == arr[i][k] && arr[i][k] == arr[i + 1][k] && arr[i][k] != ' ')
			{
				return arr[i][k];
			}
			//判断左对角线
			if (arr[i - 1][k - 1] == arr[i][k] && arr[i][k] == arr[i + 1][k + 1] && arr[i][k] != ' ')
			{
				return arr[i][k];
			}
			//判断右对角线
			if (arr[i + 1][k - 1] == arr[i][k] && arr[i][k] == arr[i - 1][k + 1] && arr[i][k] != ' ')
			{
				return arr[i][k];
			}
		}
	}


	{//判断是否满盘
		int sum = 0;
		for (int i = 0; i < ROW; i++)
		{
			for (int k = 0; i < COL; i++)
			{
				if (arr[i][k] != ' ')
				{
					++sum;
				}
			}
		}
		if (sum == ((ROW ) * (COL)))
		{
			return '$';
		}
	}

	return '@';
}
